/**
 * @project Joo: Selector Library
 * 
 * @copyright Copyright (c) 2010, Alibaba. All rights reserved.
 * @version 0.1.0
 */
 

 
var Joo = function(selector, context) {
	 	return new Joo.fn.init(selector, context);
}


Joo.fn = {
	ver : 0.1,
	/**
		* 初始化选择表达式
		* @return {object} 
	*/
	init:function(selector, context){
		 
		this.length = 0;//设置默认值
		this.domArr = [];
		
		// $(null)  $(undefined)  $("")
		if (!selector){
			return this;
		}
		
		// $(DOMElement)
		if ((typeof selector=="object" && selector.ver) || selector.nodeType){
			if(selector.ver){
				selector = selector.getItem();
			}
			
			this.domArr.push(selector);
			return Joo.fn.merge(this,this.domArr[this.domArr.length-1]);
		}
		
		// $(strings)
		if(typeof selector === "string"){
			this.Selectors = selector.replace(/(^\s*)|(\s*$)/g,"").split(/\s{1,}/);
		}
 		
		var selectLen = this.Selectors.length;
		for(var i=0;i<selectLen;i++){
			Joo.fn.filter(this.Selectors[i],this.domArr);
		}
		
		//返回原生DOM
		//return this.domArr[this.domArr.length-1];	  
		
		// 返回最后过滤的dom  有多个匹配返回Array
		return Joo.fn.merge(this,this.domArr[this.domArr.length-1]);
		
	},
	/**
		* 返回新的实例
		* @return {object}
	*/
	merge: function( initResult, domResult ) {
		if(Joo.fn.isArray(domResult)) {
			initResult.length = domResult.length;
		}else{
			initResult.length = 1;
		}		
		initResult.domArr = domResult;
		return initResult.prototype;
	},
	/**
		* 过滤选择
		* @return {object}  
	*/
	filter:function(selector,domArr){
		
		/*
			fTag 可能会存在空格或非正常字符
		*/
		var fTag = selector.charAt(0);
		/*
			fName 可能会存在空字符
		*/
		var fName = selector.substr(1,selector.length-1);

		switch(fTag){
			// HANDLE: $("#id")
			case "#":
				domArr.push(document.getElementById(fName));
			break;
			// HANDLE: $(".class")
			case ".":
				domArr.push(Joo.fn.getElemsByArray(fName,Joo.fn.getLastFilter(domArr),"getElemsByClassName"));				
			break;
			// HANDLE: $("@name")
			case "@":
				domArr.push(Joo.fn.getElemsByArray(fName,Joo.fn.getLastFilter(domArr),"getElemsByName"));
			break;			
			// HANDLE: $("TAG")
			default:		
				fTagName = selector.substr(0,selector.length);
				domArr.push(Joo.fn.getElemsByArray(fTagName,Joo.fn.getLastFilter(domArr),"getElemsByTagName"));
			break;
		}// end switch
		
	},// end filter function
	
	/**
		返回上级筛选DOM，如果存在则在上级筛选结果下进行操作
		* @return {object}  
	*/
	getLastFilter:function(domArr){ 
		var domLen = domArr.length;
		var lastDom;
		if(domLen>0){			
			lastDom = domArr[domLen-1];
		}else{
			lastDom = (navigator.userAgent.indexOf("MSIE")>0)?document:document.body;
		}
		return lastDom; 
	},
	/**
		* GetElements by Array
		* @return {object Array}  
	*/
	getElemsByArray:function(param, parentArr, func){
		var elemsArr = [];
		if(parentArr){
			if(Joo.fn.isArray(parentArr)){
				for (i=0; j=parentArr[i]; i++){
					var eArr = Joo.fn[func](param,j);					
					for(n=0; m=eArr[n]; n++){
						elemsArr.push(eArr[n]);
					}
				}
			}else{
				elemsArr = Joo.fn[func](param,parentArr);
			}
		}
		return elemsArr;
	},	
	/**
		* GetElementsByTagName
		* @return {object Array}  
	*/
	getElemsByTagName:function(tagName, parentElement){
		var result=[];
		if(parentElement){
			var elems = parentElement.getElementsByTagName(tagName);
			for(var i=0;i<elems.length;i++){
				result.push(elems[i]);
			}
		}
		return result; 
	},
	/**
		* GetElementsByClassName
		* @return {object Array}  
	*/
	getElemsByClassName:function(className, parentElement){
		var result=[];
		if(parentElement){
			if(parentElement == document.body)
				parentElement = document;
			var elems = parentElement.getElementsByTagName("*");
			for (i=0; j=elems[i]; i++){ 
				if ((" "+j.className+" ").indexOf(" "+className+" ")!=-1){ 
					result.push(j);
				}
			}
		}
		return result; 
	},
	/**
		* GetElementsByName
		* @return {object Array}  
	*/
	getElemsByName:function(name, parentElement){
		var result=[];
		if(typeof parentElement.getElementsByName !== "undefined"){
			var elems = parentElement.getElementsByName(name);
			for (i=0; j=elems[i]; i++){ 
				result.push(j);
			}
		}else{
			var elems = parentElement.getElementsByTagName("*");
			for (var i=0; i<elems.length; i++){
				if (elems[i].getAttribute("name") === name ) { 
					result.push(elems[i]);
				}
			}
		}
		return result; 
	},
	isArray: function( obj ) {
		//return (obj && obj.constructor && obj.constructor==window.Array)?true:false;
		return (obj && typeof obj.length === "number")?true:false;
	},
	getItem: function( index ) {
		var doms = this.domArr;
		if(Joo.fn.isArray(doms)){
			return doms[index];
		}else{
			return doms;
		}
	},
	/**
		* 执行扩展方法
		* @return {object}
	*/
	execFunc: function(elems, callback){
		var dArr = this.domArr;	
		//try {
			if(Joo.fn.isArray(dArr)) {
				var dLen = dArr.length;
				for(var i=0;i<dLen;i++){
					if(dArr[i]){
						callback(dArr[i]);
					}
				}
			}else{
				if(dArr){
					callback(dArr);
				}
			}			
		//}catch(e){}
		return this;
	}
 
}//end Joo.fn object

Joo.fn.init.prototype = Joo.fn;

/**
	* 扩展函数
	* @return {object}
*/

Joo.fn.setStyle = Joo.fn.css = function(style ,val){
	return this.execFunc(null,function(elems){elems.style[style] = val});
}

Joo.fn.html = function(val){
	return this.execFunc(null,function(elems){elems.innerHTML = val});
}

Joo.fn.attr = function(attr,val){
	return this.execFunc(null,function(elems){elems.setAttribute(attr,val)});
}

Joo.fn.value = Joo.fn.val = function(val){
	return this.execFunc(null,function(elems){elems.value = val});
}

 
 
 
